#include "tracexportsettingspage.h"
#include "tracexportsettingswidget.h"
#include "tracsettings.h"

#include "xmlrpcbase64.h"
#include "xmlrpcconnection.h"
#include "xmlrpcparser.h"
#include "xmlrpcrequest.h"
#include "xmlrpcresponse.h"

#include <QtGui/QVBoxLayout>
#include <QtGui/QTextEdit>
#include <QtGui/QMessageBox>
#include <QtCore/QDebug>
#include <QtCore/QUrl>

using XmlRpc::XmlRpcConnection;
using XmlRpc::XmlRpcRequest;
using XmlRpc::XmlRpcBase64;

namespace Trac {
namespace Internal {

TracExportSettingsPage::TracExportSettingsPage(QWidget *parent) :
    QWizardPage(parent),
    m_widget(new TracExportSettingsWidget()),
    m_connection(new XmlRpcConnection)
{
    connect(m_widget, SIGNAL(validChanged(bool)), this, SIGNAL(completeChanged()));

    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(m_widget);
    setLayout(layout);
    setTitle(tr("Export settings"));
    setSubTitle(tr("Export settings"));

}

TracExportSettingsPage::~TracExportSettingsPage()
{

}

void TracExportSettingsPage::initializePage()
{

}

bool TracExportSettingsPage::isComplete() const
{
    return m_widget->isValid();
}

bool TracExportSettingsPage::validatePage()
{
    return createProject();
}

bool TracExportSettingsPage::createProject()
{
    XmlRpc::XmlRpcResponse *response;
    QMessageBox msgBox;
    QString msgBoxString;

    m_connection->setUrl(QUrl(TracSettings::instance()->hostName()));
    QVariantList parameters;

    m_request.setMethod(Constants::RPC_CREATEPROJECT);

    parameters << TracSettings::instance()->projectIdentifier()
            << TracSettings::instance()->projectName()
            << TracSettings::instance()->projectDescription()
            << QString(TracSettings::instance()->isPublicProject() ? "true" : "false")
            << QString("versioncontrol|" + TracSettings::instance()->binary());

    m_request.setParams(parameters);

    // Remove comments to enable project creation to host
    /*
    response = m_connection->execute(m_request);

    if(response->error()) {

        msgBoxString = QString(tr("ERROR (%1): %2")).arg(response->errorCode())
                      .arg(response->errorString());

        msgBox.setText("Project creation failed");
        msgBox.setInformativeText(msgBoxString);
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        msgBox.exec();
        return false;
    }
    else {

        msgBox.setText("Project created succesfully");
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        msgBox.exec();
        return true;
    }*/

    return true;

}


} // namespace Internal
} // namespace Trac


